#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

void printCapacity(const vector<int> &vec)
{
    cout << "vec.size() = " << vec.size() << endl;
    cout << "vec.capacity() = " << vec.capacity() << endl;
}

void test()
{
    vector<int> number = {1, 3, 5, 7, 8, 6, 4, 9, 4, 5};
    display(number);
    printCapacity(number);

    cout << endl << "在vector的尾部进行插入与删除" << endl;
    number.push_back(10);
    number.push_back(20);
    display(number);
    printCapacity(number);

    //size() = m = 12, capacity() = n = 20, 待插入的元素个数 t
    //1、当 t < n - m,此时就不需要扩容
    //2、当 n - m < t < m,就会按照2 * m进行扩容
    //3、当 n - m < t, m < t < n,就会按照m + t进行扩容
    //3、当 n - m < t, n < t,就会按照m + t进行扩容
    cout << endl << "在vector的任意位置进行插入" << endl;
    auto it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, 80, 10);
    display(number);
    cout << "*it = " << *it << endl;
    printCapacity(number);

    cout << endl << endl;
    vector<int> vec = {111, 555, 333};
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, vec.begin(), vec.end());
    display(number);
    cout << "*it = " << *it << endl;
    printCapacity(number);
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

